package co.th.bjc.md.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import co.th.bjc.md.dao.Product;
import co.th.bjc.md.process.service.CallService;
import co.th.bjc.md.service.interfaces.ProductManageService;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class MemberManageServlet
 */
public class MemberManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MemberManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		JSONArray jsonObject = new JSONArray();
		boolean status = false;
		String action = request.getParameter("action")==null?"":request.getParameter("action");
		
		if(action.equals("SEARCH")){
			PrintWriter out = response.getWriter();
			jsonObject = this.getSearchResult(request, response);
			out.print(jsonObject);
		}else if(action.equals("DELETE")){
			// Delete process
			if(this.delete(request, response)){
				PrintWriter out = response.getWriter();
				status = true;
				jsonObject = this.getSearchResult(request, response);
				jsonObject.add(status);
				out.print(jsonObject);
			}
		}else if(action.equals("GETEDIT")){
			PrintWriter out = response.getWriter();
			jsonObject = getEditItem(request, response);
			status = true;
			jsonObject.add(status);
			out.print(jsonObject);
		}else{
			getServletContext().getRequestDispatcher("/jsp/datamanage/manage_member_main.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private JSONArray getSearchResult(HttpServletRequest request, HttpServletResponse response) {
		ProductManageService prod = (ProductManageService) CallService.getService("productmanage");
		String productName = request.getParameter("srh_product_name") == null?"":request.getParameter("srh_product_name");
		String supplierCode = request.getParameter("srh_supplier_code") == null || request.getParameter("srh_supplier_code").equals("")?"0":request.getParameter("srh_supplier_code");
		List<Product> list = prod.getAllProduct(productName, BigDecimal.valueOf(Long.parseLong(supplierCode)));
		JSONArray jsonObject = JSONArray.fromObject(list);
		return jsonObject;
	}
	
	private boolean insert(HttpServletRequest request, HttpServletResponse response) {
		return false;
	}
	
	private boolean update(HttpServletRequest request, HttpServletResponse response) {
		return false;
	}
	
	private boolean delete(HttpServletRequest request, HttpServletResponse response){
		return false;
	}
	
	private JSONArray getEditItem(HttpServletRequest request, HttpServletResponse response){
		return null;
	}

}
